package CTRLayer;
import ModelLayer.*;
import java.util.ArrayList;
/**
 * A class to control the creation and management of orders.
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class OrderCtr
{
    // Instance variables
    private OrderContainer orderContainer;
    private ProductCtr productCtr;
    private CorporateCustomerCtr cCCtr;

    /**
     * Constructor for objects of class OrderController
     */
    public OrderCtr()
    {
        orderContainer = OrderContainer.getInstance();
        productCtr = new ProductCtr();
        cCCtr = new CorporateCustomerCtr();
    }
    
    /**
     * Creates a new order using the createOrder method, adds a customer to it found by customer id, 
     * adds the order to the order container and returns a string containing the new orders orderID
     */
    public String addOrder(String customerType, String customerID, boolean wantedDelivery)
    {
        CorporateCustomer customer = cCCtr.getCorporateCustomer(customerID);
        Order addOrder = createOrder(customerType, customer, wantedDelivery);
        orderContainer.addOrder(addOrder);
        return addOrder.getOrderID();
    }
    
    /**
     * Creates a new order object
     */
    private Order createOrder(String customerType, Customer customer, boolean wantedDelivery)
    {
        Order order = new Order(customerType, customer, wantedDelivery);
        return order;
    }
    
    /**
     * Creates a new partOrder object, adds a product to it found by product EAN 
     * and adds the new partOrder to an order from the order container found by orderID
     */
    public double createPartOrder(int amount, String EAN, String orderID)
    {
        Product product = productCtr.findProductByEAN(EAN);
        PartOrder partOrder = new PartOrder(amount, product);
        getOrder(orderID).addPartOrder(partOrder);
        return partOrder.calculateTotalPartOrderPrice();
    }
    
    /**
     * Returns an order from the order container found by orderID
     */
    public Order getOrder(String orderID)
    {
        return orderContainer.getOrder(orderID);
    }
    
    /**
     * Returns a string containing details for an order found in the order container by orderID
     */
    public void getOrderDetails(String orderID)
    {
        orderContainer.getOrder(orderID).getDetailsString();
    }
    
    /**
     * Returns an instance of the productCtr class
     */
    public ProductCtr getproductCtr()
    {
        return productCtr;
    }
    
    /**
     * Returns an instance of the corporateCustomerCtr class
     */
    public CorporateCustomerCtr getCorporateCustomerCtr()
    {
        return cCCtr;
    }
    
    /**
     * Removes an order from the order container determined by orderID
     */
    public void removeOrder(String orderID)
    {
        orderContainer.removeOrder(orderID);
    }
    
    /**
     * Returns the total price of an order
     */
    public double totalOrderPrice(String orderID)
    {
        double totalPrice = 0;
        totalPrice = orderContainer.getOrder(orderID).calculateTotalPrice();
        return totalPrice;
    }
    
    public void getOrderCustomer(String orderID)
    {
    	orderContainer.getOrder(orderID).getCustomer();
    }
}
